package com.clkj.cldisabledmanclient.mvp.recovertrainapply;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.adapter.jaylv.JayLvBaseAdapter;
import com.clkj.cldisabledmanclient.adapter.jaylv.ViewHolder;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.bean.InstitutionBean;
import com.clkj.cldisabledmanclient.bean.PersonInfo;
import com.clkj.cldisabledmanclient.constant.ShareKey;
import com.clkj.cldisabledmanclient.eventbus.LinkmanEvent;
import com.clkj.cldisabledmanclient.mvp.SuccessActivity;
import com.clkj.cldisabledmanclient.mvp.ViewPhotoActivity;
import com.clkj.cldisabledmanclient.util.CommonUtil;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;
import com.clkj.cldisabledmanclient.widget.GridViewForScrollView;
import com.clkj.cldisabledmanclient.widget.ListViewForScrollView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class RecoverTrainApplyActivity extends BaseActivity implements View.OnClickListener, RecoverTrainApplyContract.View {

    private RelativeLayout mRlTitle;
    private ImageView mIvBack;
    private TextView mTvTitle;
    private TextView mTvRight;
    private ImageView mIvAvator;
    private TextView mTvName;
    private TextView mTvSex;
    private TextView mTvBirthday;
    private TextView mTvIdcard;
    private TextView mTvAssressLabel;
    private TextView mTvQuxian;
    private EditText mEtDetailAddress;
    private TextView mTvCanjiType;
    private ListViewForScrollView mLvLinkers;
    private TextView mTvTipAdd;
    private TextView mTvFamilyType;
    private TextView mTvEconomicSource;
    private EditText mEtAverageSalary;
    private EditText mEtApplyReason;
    private ImageView mIvAddLinker;
    private LinearLayout mLyGuardian;
    private EditText mEtGuardianName;
    private EditText mEtGuardianRelationship;
    private EditText mEtGuardianCompany;
    private EditText mEtGuardianTel;
    private LinearLayout mLyGuardian2;
    private EditText mEtGuardianName2;
    private EditText mEtGuardianRelationship2;
    private EditText mEtGuardianCompany2;
    private EditText mEtGuardianTel2;
    private TextView mTvOrganize;
    private ImageView mIvAddPhoto;
    private TextView mTvPhotostate;
    private GridViewForScrollView mGvPhotos;
    private Button mBtSubmit;

    private ProgressDialog progressDialog;
    private RecoverTrainApplyContract.Presenter mPresenter;
    private PersonInfo personInfo;

    private String idCard;

    private List<String> regionStrs = new ArrayList<>();
    private String chooseRegion;
    private List<String> disableTypeStrs = new ArrayList<>();
    private String chooseDisableType;
    private List<String> familyTypeStrs = new ArrayList<>();
    private String chooseFamilyType;
    private List<String> economicTypeStrs = new ArrayList<>();
    private String chooseEconomicType;
    List<InstitutionBean> institutionBeanList = new ArrayList<>();
    private List<String> organizeStrs = new ArrayList<>();
    private String chooseOrganize;

    private int REQUEST_PHOTO = 0x111;
    private String personnelImgage;
    private String guardianIDCard;
    private String guardianResidenceBooklet;
    private String childrenResidenceBooklet;
    private String lowAssurance;
    private String disabilityCertificate;
    private String hospitalProve;

    private JayLvBaseAdapter<String> photosAdapter;
    private List<String> photosList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recover_train_apply);
        initTitle(null, null, "残疾人康复训练申请", true, null);
        initView();
        initData();
        assignView();

        //获取个人信息
        getPersonInfo();

        //获取区县信息
        getRegionXian();
        //获取残疾类别
        getDisableType();

        //获取家庭类别
        getFamilyTypes();
        //经济来源
        getEconomicSource();
    }

    @Override
    public void initData() {

        EventBus.getDefault().register(this);
        idCard = (String) CommonUtil.getShareValue(ShareKey.USER_IDCARD, "");
        mPresenter = new RecoverTrainApplyPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void assignView() {

        mTvQuxian.setOnClickListener(this);
        mIvAddLinker.setOnClickListener(this);
        mTvCanjiType.setOnClickListener(this);
        mTvOrganize.setOnClickListener(this);
        mTvFamilyType.setOnClickListener(this);
        mTvEconomicSource.setOnClickListener(this);

        mIvAddPhoto.setOnClickListener(this);
        mBtSubmit.setOnClickListener(this);


        photosAdapter = new JayLvBaseAdapter<String>(this, photosList, R.layout.item_photo) {
            @Override
            public void convert(ViewHolder helper, int position, String item) {

                ImageView ivPhoto = helper.getView(R.id.iv_photo);
                RequestOptions requestOptions = new RequestOptions().centerCrop().placeholder(R.mipmap.ico_data_empty)
                        .error(R.mipmap.ico_data_empty).centerCrop();
                Glide.with(RecoverTrainApplyActivity.this).load(photosList.get(position)).apply(requestOptions)
                        .into(ivPhoto);

                ivPhoto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ArrayList<String> imgs = new ArrayList<String>();
                        for (String path : photosList) {
                            imgs.add(path);
                        }
                        startActivity(ViewPhotoActivity.newIntent(RecoverTrainApplyActivity.this, imgs, position, true));
                    }
                });
            }
        };

        mGvPhotos.setAdapter(photosAdapter);
    }

    @Override
    public void initView() {

        mRlTitle = findViewById(R.id.rl_title);
        mIvBack = findViewById(R.id.iv_back);
        mTvTitle = findViewById(R.id.tv_title);
        mTvRight = findViewById(R.id.tv_right);
        mIvAvator = findViewById(R.id.iv_avator);
        mTvName = findViewById(R.id.tv_name);
        mTvSex = findViewById(R.id.tv_sex);
        mTvBirthday = findViewById(R.id.tv_birthday);
        mTvIdcard = findViewById(R.id.tv_idcard);
        mTvAssressLabel = findViewById(R.id.tv_assress_label);
        mTvQuxian = findViewById(R.id.tv_quxian);
        mEtDetailAddress = findViewById(R.id.et_detail_address);
        mTvCanjiType = findViewById(R.id.tv_canjiType);
        mLvLinkers = findViewById(R.id.lv_linkers);
        mTvTipAdd = findViewById(R.id.tv_tip_add);
        mTvFamilyType = findViewById(R.id.tv_familyType);
        mTvEconomicSource = findViewById(R.id.tv_economicSource);
        mEtAverageSalary = findViewById(R.id.et_average_salary);
        mEtApplyReason = findViewById(R.id.et_apply_reason);
        mIvAddLinker = findViewById(R.id.iv_add_linker);
        mLyGuardian = findViewById(R.id.ly_guardian);
        mEtGuardianName = findViewById(R.id.et_guardianName);
        mEtGuardianRelationship = findViewById(R.id.et_guardianRelationship);
        mEtGuardianCompany = findViewById(R.id.et_guardianCompany);
        mEtGuardianTel = findViewById(R.id.et_guardianTel);
        mLyGuardian2 = findViewById(R.id.ly_guardian2);
        mEtGuardianName2 = findViewById(R.id.et_guardianName2);
        mEtGuardianRelationship2 = findViewById(R.id.et_guardianRelationship2);
        mEtGuardianCompany2 = findViewById(R.id.et_guardianCompany2);
        mEtGuardianTel2 = findViewById(R.id.et_guardianTel2);
        mTvOrganize = findViewById(R.id.tv_organize);
        mIvAddPhoto = findViewById(R.id.iv_addPhoto);
        mTvPhotostate = findViewById(R.id.tv_photostate);
        mGvPhotos = findViewById(R.id.gv_photos);
        mBtSubmit = findViewById(R.id.bt_submit);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //添加联系人
            case R.id.iv_add_linker:

                if (mLyGuardian.getVisibility() == View.VISIBLE && mLyGuardian2.getVisibility() == View.VISIBLE) {

                    ToastUtil.showShort(RecoverTrainApplyActivity.this, "监护人只能添加两条");
                    return;
                }
                DialogFragmentForAddLinker fragmentForAddLinker = DialogFragmentForAddLinker.getInstance("");
                fragmentForAddLinker.show(getSupportFragmentManager(), "DialogFragmentForAddLinker");
                fragmentForAddLinker.setCancelable(false);
                break;
            //区县
            case R.id.tv_quxian:

                if (regionStrs.size() == 0) {
                    ToastUtil.showShort(RecoverTrainApplyActivity.this, "未获取到区县信息");
                    mPresenter.getRegionXian();
                    return;
                }


                OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        mTvQuxian.setText(regionStrs.get(options1));
                        chooseRegion = regionStrs.get(options1);
                    }

                }).build();
                pvOptions.setPicker(regionStrs);
                pvOptions.show();
                break;
            //残疾类别
            case R.id.tv_canjiType:

                CommonUtil.hideInput(RecoverTrainApplyActivity.this);

                if (disableTypeStrs.size() == 0) {
                    ToastUtil.showShort(RecoverTrainApplyActivity.this, "未获取到残疾类别");
                    mPresenter.getDisableType();
                    return;
                }

                OptionsPickerView pvOptions2 = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        mTvCanjiType.setText(disableTypeStrs.get(options1));
                        chooseDisableType = disableTypeStrs.get(options1);

                        mTvOrganize.setText("");
                        chooseOrganize = "";

                        //获取意向机构
                        getOrgnizeByDiableType(chooseDisableType);
                    }

                }).build();
                pvOptions2.setPicker(disableTypeStrs);
                pvOptions2.show();
                break;
            //意向机构
            case R.id.tv_organize:
                CommonUtil.hideInput(RecoverTrainApplyActivity.this);
                if (TextUtils.isEmpty(mTvCanjiType.getText().toString())) {
                    ToastUtil.showShort(RecoverTrainApplyActivity.this, "请先选择残疾类型");
                    return;
                }

                if (organizeStrs.size() == 0) {
                    ToastUtil.showShort(RecoverTrainApplyActivity.this, "未获取到意向机构");
                    return;
                }


                OptionsPickerView pvOptions3 = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        mTvOrganize.setText(organizeStrs.get(options1));
                        //chooseOrganize传递的是id
                        chooseOrganize = institutionBeanList.get(options1).getInstitutionId();
                    }

                }).build();
                pvOptions3.setPicker(organizeStrs);
                pvOptions3.show();
                break;

            //家庭类别
            case R.id.tv_familyType:

                CommonUtil.hideInput(RecoverTrainApplyActivity.this);
                if (familyTypeStrs.size() == 0) {
                    ToastUtil.showShort(RecoverTrainApplyActivity.this, "未获取到家庭类别");
                    mPresenter.getFamilyTypes();
                    return;
                }


                OptionsPickerView pvOptions4 = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        mTvFamilyType.setText(familyTypeStrs.get(options1));
                        chooseFamilyType = familyTypeStrs.get(options1);
                    }

                }).build();
                pvOptions4.setPicker(familyTypeStrs);
                pvOptions4.show();
                break;


            //经济来源
            case R.id.tv_economicSource:

                CommonUtil.hideInput(RecoverTrainApplyActivity.this);
                if (economicTypeStrs.size() == 0) {
                    ToastUtil.showShort(RecoverTrainApplyActivity.this, "未获取到经济来源");
                    mPresenter.getEconomicSource();
                    return;
                }


                OptionsPickerView pvOptions5 = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        mTvEconomicSource.setText(economicTypeStrs.get(options1));
                        chooseEconomicType = economicTypeStrs.get(options1);
                    }

                }).build();
                pvOptions5.setPicker(economicTypeStrs);
                pvOptions5.show();
                break;
            //去上传照片
            case R.id.iv_addPhoto:

                Intent toUpPhoto = new Intent(RecoverTrainApplyActivity.this, AddKfxlApplyFileActivity.class);
                toUpPhoto.putExtra("grzpnewPath", personnelImgage);
                toUpPhoto.putExtra("jhrsfznewPath", guardianIDCard);
                toUpPhoto.putExtra("jhrhkbnewPath", guardianResidenceBooklet);
                toUpPhoto.putExtra("ethkbnewPath", childrenResidenceBooklet);
                toUpPhoto.putExtra("dbzhpkzmnewPath", lowAssurance);
                toUpPhoto.putExtra("cjznewPath", disabilityCertificate);
                toUpPhoto.putExtra("yyzmnewPath", hospitalProve);
                startActivityForResult(toUpPhoto, REQUEST_PHOTO);
                break;
            //提交表单
            case R.id.bt_submit:

                submitApply();
                break;
            default:
                break;
        }
    }


    @Override
    public void getPersonInfo() {
        mPresenter.getPersonInfo(idCard);
    }

    @Override
    public void getRegionXian() {

        mPresenter.getRegionXian();
    }

    @Override
    public void getDisableType() {
        mPresenter.getDisableType();
    }

    @Override
    public void getFamilyTypes() {
        mPresenter.getFamilyTypes();
    }

    @Override
    public void getEconomicSource() {

        mPresenter.getEconomicSource();
    }

    @Override
    public void getOrgnizeByDiableType(String disabilityCategory) {
        mPresenter.getOrgnizeByDiableType(disabilityCategory);
    }

    @Override
    public void getPersonInfoSuccess(PersonInfo personInfo) {

        this.personInfo = personInfo;

        mTvName.setText("姓名：" + personInfo.getName());
        mTvSex.setText("性别：" + personInfo.getGender());
        mTvBirthday.setText("出生年月：" + personInfo.getBirthday());
        mTvIdcard.setText("身份证号：" + personInfo.getIdCard());


        if (!TextUtils.isEmpty(personInfo.getGuardianName())) {
            mTvTipAdd.setVisibility(View.GONE);
            mLyGuardian.setVisibility(View.VISIBLE);
            mEtGuardianName.setText(personInfo.getGuardianName());
            mEtGuardianRelationship.setText(personInfo.getGuardianRelationship());
            mEtGuardianCompany.setText(personInfo.getGuardianCompany());
            mEtGuardianTel.setText(personInfo.getGuardianTel());
        }

        if (!TextUtils.isEmpty(personInfo.getGuardianName2())) {
            mTvTipAdd.setVisibility(View.GONE);
            mLyGuardian2.setVisibility(View.VISIBLE);
            mEtGuardianName2.setText(personInfo.getGuardianName2());
            mEtGuardianRelationship2.setText(personInfo.getGuardianRelationship2());
            mEtGuardianCompany2.setText(personInfo.getGuardianCompany2());
            mEtGuardianTel2.setText(personInfo.getGuardianTel2());
        }
    }

    @Override
    public void getPersonInfoError(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void getRegionXianSuccess(String[] regions) {

        regionStrs = Arrays.asList(regions);
    }

    @Override
    public void getRegionXianError(String tip) {

        ToastUtil.showShort(this, tip);
    }

    @Override
    public void getDisableTypeSuccess(String[] disabletypes) {

        disableTypeStrs = Arrays.asList(disabletypes);
    }

    @Override
    public void getDisableTypeError(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void getEconomicSourceSuccess(String[] economicSources) {

        economicTypeStrs = Arrays.asList(economicSources);
    }

    @Override
    public void getEconomicSourceError(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void getFamilyTypesSuccess(String[] familytypes) {

        familyTypeStrs = Arrays.asList(familytypes);
    }

    @Override
    public void getFamilyTypesError(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void getOrgnizeByDiableTypeSuccess(List<InstitutionBean> institutionBeans) {

        institutionBeanList.clear();
        institutionBeanList.addAll(institutionBeans);
        if (institutionBeanList.size() > 0) {
            for (InstitutionBean institutionBean : institutionBeans) {
                organizeStrs.add(institutionBean.getInstitutionName());
            }
        }
    }

    @Override
    public void getOrgnizeByDiableTypeError(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void submitApply() {

        if (personInfo == null) {
            ToastUtil.showShort(RecoverTrainApplyActivity.this, "未获取到个人信息，无法提交");
            return;
        }

        if (TextUtils.isEmpty(chooseRegion)) {
            ToastUtil.showShort(RecoverTrainApplyActivity.this, "请选择区县");
            return;
        }

        if (TextUtils.isEmpty(mEtDetailAddress.getText().toString())) {
            ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写详细地址");
            return;
        }

        if (TextUtils.isEmpty(chooseDisableType)) {
            ToastUtil.showShort(RecoverTrainApplyActivity.this, "请选择残疾类型");
            return;
        }


        if (TextUtils.isEmpty(chooseOrganize)) {
            ToastUtil.showShort(RecoverTrainApplyActivity.this, "请选择意向机构");
            return;
        }

        //假如监护人1不是空
        if (mLyGuardian.getVisibility() == View.VISIBLE) {

            if (TextUtils.isEmpty(mEtGuardianName.getText().toString())) {
                ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写监护人姓名");
                return;
            }
            if (TextUtils.isEmpty(mEtGuardianRelationship.getText().toString())) {
                ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写与监护人关系");
                return;
            }
            if (TextUtils.isEmpty(mEtGuardianCompany.getText().toString())) {
                ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写监护人所在单位");
                return;
            }
            if (TextUtils.isEmpty(mEtGuardianTel.getText().toString())) {
                ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写监护人联系电话");
                return;
            }

            if(!CommonUtil.isMobile2(mEtGuardianTel.getText().toString()))
            {
                ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写正确的监护人联系电话");
                return;
            }

        }

        //假如监护人2不是空
        if (mLyGuardian2.getVisibility() == View.VISIBLE) {

            if (TextUtils.isEmpty(mEtGuardianName2.getText().toString())) {
                ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写监护人姓名");
                return;
            }
            if (TextUtils.isEmpty(mEtGuardianRelationship2.getText().toString())) {
                ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写与监护人关系");
                return;
            }
            if (TextUtils.isEmpty(mEtGuardianCompany2.getText().toString())) {
                ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写监护人所在单位");
                return;
            }
            if (TextUtils.isEmpty(mEtGuardianTel2.getText().toString())) {
                ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写监护人联系电话");
                return;
            }

            if(!CommonUtil.isMobile2(mEtGuardianTel2.getText().toString()))
            {
                ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写正确的监护人联系电话");
                return;
            }
        }

        if (TextUtils.isEmpty(chooseFamilyType)) {
            ToastUtil.showShort(RecoverTrainApplyActivity.this, "请选择家庭类别");
            return;
        }
        if (TextUtils.isEmpty(chooseEconomicType)) {
            ToastUtil.showShort(RecoverTrainApplyActivity.this, "请选择经济来源");
            return;
        }

        if (TextUtils.isEmpty(mEtAverageSalary.getText().toString())) {
            ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写人均年收入");
            return;
        }

        if (TextUtils.isEmpty(mEtApplyReason.getText().toString())) {
            ToastUtil.showShort(RecoverTrainApplyActivity.this, "请填写申请理由");
            return;
        }


        if (personnelImgage == null) {
            ToastUtil.showShort(RecoverTrainApplyActivity.this, "请上传申请材料");
            return;
        }


        //下面开始真正提交信息
        RequestBody areaBody = RequestBody.create(MediaType.parse("multipart/form-data"), chooseRegion);
        RequestBody countryBody = RequestBody.create(MediaType.parse("multipart/form-data"), "");
        RequestBody nameBody = RequestBody.create(MediaType.parse("multipart/form-data"), personInfo.getName());

        RequestBody genderBody;
        if (TextUtils.isEmpty(personInfo.getGender())) {
            genderBody = RequestBody.create(MediaType.parse("multipart/form-data"), "");
        } else {
            genderBody = RequestBody.create(MediaType.parse("multipart/form-data"), personInfo.getGender());
        }
        RequestBody birthdayBody;
        if (TextUtils.isEmpty(personInfo.getBirthday())) {
            birthdayBody = RequestBody.create(MediaType.parse("multipart/form-data"), "");
        } else {
            birthdayBody = RequestBody.create(MediaType.parse("multipart/form-data"), personInfo.getBirthday());
        }
        RequestBody disabilityCategoryBody = RequestBody.create(MediaType.parse("multipart/form-data"), chooseDisableType);
        RequestBody IdCardBody = RequestBody.create(MediaType.parse("multipart/form-data"), personInfo.getIdCard());
        RequestBody addressBody = RequestBody.create(MediaType.parse("multipart/form-data"), mEtDetailAddress.getText().toString());
        //第一监护人
        RequestBody guardianNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), mEtGuardianName.getText().toString());
        RequestBody guardianRelationshipBody = RequestBody.create(MediaType.parse("multipart/form-data"), mEtGuardianRelationship.getText().toString());
        RequestBody guardianCompanyBody = RequestBody.create(MediaType.parse("multipart/form-data"), mEtGuardianCompany.getText().toString());
        RequestBody guardianTelBody = RequestBody.create(MediaType.parse("multipart/form-data"), mEtGuardianTel.getText().toString());
        //第二监护人
        RequestBody guardianName2Body = RequestBody.create(MediaType.parse("multipart/form-data"), mEtGuardianName2.getText().toString());
        RequestBody guardianRelationship2Body = RequestBody.create(MediaType.parse("multipart/form-data"), mEtGuardianRelationship2.getText().toString());
        RequestBody guardianCompany2Body = RequestBody.create(MediaType.parse("multipart/form-data"), mEtGuardianCompany2.getText().toString());
        RequestBody guardianTel2Body = RequestBody.create(MediaType.parse("multipart/form-data"), mEtGuardianTel2.getText().toString());

        RequestBody familyCategoryBody = RequestBody.create(MediaType.parse("multipart/form-data"), chooseFamilyType);
        RequestBody economicSourceBody = RequestBody.create(MediaType.parse("multipart/form-data"), chooseEconomicType);
        RequestBody personnelAnnualIncomeBody = RequestBody.create(MediaType.parse("multipart/form-data"), mEtAverageSalary.getText().toString());
        RequestBody institutionIdBody = RequestBody.create(MediaType.parse("multipart/form-data"), chooseOrganize);
        RequestBody reasonDemandBody = RequestBody.create(MediaType.parse("multipart/form-data"), mEtApplyReason.getText().toString());


        //依次上传7张图片
        MultipartBody.Part personnelImgageFile = null;
        MultipartBody.Part guardianIDCardFile = null;
        MultipartBody.Part guardianResidenceBookletFile = null;
        MultipartBody.Part childrenResidenceBookletFile = null;
        MultipartBody.Part lowAssuranceFile = null;
        MultipartBody.Part disabilityCertificateFile = null;
        MultipartBody.Part hospitalProveFile = null;

        if (!TextUtils.isEmpty(personnelImgage)) {
            File image = new File(personnelImgage);
          /*  Uri uri = Uri.fromFile(image);
            File f = CommonUtil.scal(uri);*/
            RequestBody picFile = RequestBody.create(MediaType.parse(CommonUtil.guessMimeType(image.getName())), image);
            personnelImgageFile = MultipartBody.Part.createFormData("personnelImgage", image.getName(), picFile);
        }
        if (!TextUtils.isEmpty(guardianIDCard)) {
            File image = new File(guardianIDCard);
            RequestBody picFile = RequestBody.create(MediaType.parse(CommonUtil.guessMimeType(image.getName())), image);
            guardianIDCardFile = MultipartBody.Part.createFormData("guardianIDCard", image.getName(), picFile);
        }

        if (!TextUtils.isEmpty(guardianResidenceBooklet)) {
            File image = new File(guardianResidenceBooklet);
            RequestBody picFile = RequestBody.create(MediaType.parse(CommonUtil.guessMimeType(image.getName())), image);
            guardianResidenceBookletFile = MultipartBody.Part.createFormData("guardianResidenceBooklet", image.getName(), picFile);
        }


        if (!TextUtils.isEmpty(childrenResidenceBooklet)) {
            File image = new File(childrenResidenceBooklet);
            RequestBody picFile = RequestBody.create(MediaType.parse(CommonUtil.guessMimeType(image.getName())), image);
            childrenResidenceBookletFile = MultipartBody.Part.createFormData("childrenResidenceBooklet", image.getName(), picFile);
        }

        if (!TextUtils.isEmpty(lowAssurance)) {
            File image = new File(lowAssurance);
            RequestBody picFile = RequestBody.create(MediaType.parse(CommonUtil.guessMimeType(image.getName())), image);
            lowAssuranceFile = MultipartBody.Part.createFormData("lowAssurance", image.getName(), picFile);
        }


        if (!TextUtils.isEmpty(hospitalProve)) {
            File image = new File(hospitalProve);
            RequestBody picFile = RequestBody.create(MediaType.parse(CommonUtil.guessMimeType(image.getName())), image);
            hospitalProveFile = MultipartBody.Part.createFormData("hospitalProve", image.getName(), picFile);
        }

        if (!TextUtils.isEmpty(disabilityCertificate)) {
            File image = new File(disabilityCertificate);
            RequestBody picFile = RequestBody.create(MediaType.parse(CommonUtil.guessMimeType(image.getName())), image);
            disabilityCertificateFile = MultipartBody.Part.createFormData("disabilityCertificate", image.getName(), picFile);

        }

        mPresenter.addApply(areaBody, countryBody, nameBody, genderBody, birthdayBody, disabilityCategoryBody, IdCardBody, addressBody, guardianNameBody,
                guardianRelationshipBody, guardianCompanyBody, guardianTelBody, guardianName2Body, guardianRelationship2Body, guardianCompany2Body, guardianTel2Body,
                familyCategoryBody, economicSourceBody, personnelAnnualIncomeBody, institutionIdBody, reasonDemandBody
                , personnelImgageFile, guardianIDCardFile, guardianResidenceBookletFile, childrenResidenceBookletFile,
                lowAssuranceFile, disabilityCertificateFile, hospitalProveFile);

    }

    @Override
    public void addApplySuccess(String result) {

        startActivity(new Intent(RecoverTrainApplyActivity.this, SuccessActivity.class));
        finish();

    }

    @Override
    public void addApplyError(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void setPresenter(RecoverTrainApplyContract.Presenter presenter) {

        this.mPresenter = presenter;
    }

    @Override
    public void showLoading() {

        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(true);
            //设置点击进度对话框外的区域对话框不消失
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setMessage("正在加载...");
            progressDialog.show();
        }


    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetLinkman(LinkmanEvent linkmanEvent) {

        if (mLyGuardian.getVisibility() == View.GONE) {
            mLyGuardian.setVisibility(View.VISIBLE);
            mEtGuardianName.setText(linkmanEvent.getName());
            mEtGuardianRelationship.setText(linkmanEvent.getRelatiobship());
            mEtGuardianCompany.setText(linkmanEvent.getCompany());
            mEtGuardianTel.setText(linkmanEvent.getPhone());
        }

        if (mLyGuardian2.getVisibility() == View.GONE) {
            mLyGuardian2.setVisibility(View.VISIBLE);
            mEtGuardianName2.setText(linkmanEvent.getName());
            mEtGuardianRelationship2.setText(linkmanEvent.getRelatiobship());
            mEtGuardianCompany2.setText(linkmanEvent.getCompany());
            mEtGuardianTel2.setText(linkmanEvent.getPhone());
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //获取返回过来的图片信息
        if (requestCode == REQUEST_PHOTO && resultCode == RESULT_OK) {

            mTvPhotostate.setText("已选择材料");
            List<String> photos = new ArrayList<>();
            personnelImgage = data.getStringExtra("grzpnewPath");
            guardianIDCard = data.getStringExtra("jhrsfznewPath");
            guardianResidenceBooklet = data.getStringExtra("jhrhkbnewPath");
            childrenResidenceBooklet = data.getStringExtra("ethkbnewPath");
            lowAssurance = data.getStringExtra("dbzhpkzmnewPath");
            disabilityCertificate = data.getStringExtra("cjznewPath");
            hospitalProve = data.getStringExtra("yyzmnewPath");
            if (!TextUtils.isEmpty(personnelImgage)) {
                photos.add(personnelImgage);
            }
            if (!TextUtils.isEmpty(guardianIDCard)) {
                photos.add(guardianIDCard);
            }
            if (!TextUtils.isEmpty(guardianResidenceBooklet)) {
                photos.add(guardianResidenceBooklet);
            }
            if (!TextUtils.isEmpty(childrenResidenceBooklet)) {
                photos.add(childrenResidenceBooklet);
            }
            if (!TextUtils.isEmpty(lowAssurance)) {
                photos.add(lowAssurance);
            }
            if (!TextUtils.isEmpty(disabilityCertificate)) {
                photos.add(disabilityCertificate);
            }
            if (!TextUtils.isEmpty(hospitalProve)) {
                photos.add(hospitalProve);
            }

            photosList.clear();
            photosList.addAll(photos);
            photosAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
